package com.ruoyi.web.controller.comlbs;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.comlbs.BindStatusEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.VehicleManagement;
import com.ruoyi.system.domain.request.vehicle.NumberPlateExistParam;
import com.ruoyi.system.domain.request.vehicle.VehicleSearchParam;
import com.ruoyi.system.domain.vo.equipment.EquipmentInfo;
import com.ruoyi.system.service.IEquipmentManagementService;
import com.ruoyi.system.service.IVehicleManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 监管对象管理Controller
 *
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/vehicleManagement")
public class VehicleManagementController extends BaseController {
    @Autowired
    private IVehicleManagementService vehicleManagementService;
    @Autowired
    private IEquipmentManagementService equipmentManagementService;

    @ApiOperation("监管对象管理列表")
    @ApiOperationSupport(includeParameters = {"pageNum","pageSize","companyId","numberPlate","bindStatus","vehicleTaskStatus"})
    @GetMapping("/list")
    public TableDataInfo list(VehicleManagement vehicleManagement) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((vehicleManagement.getCompanyId()))) {
            vehicleManagement.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<VehicleManagement> list = vehicleManagementService.selectList(vehicleManagement);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @ApiOperation("监管对象管理列表导出")
    @ApiOperationSupport(includeParameters = {"companyId","numberPlate","bindStatus"})
    @Log(title = "监管对象管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehicleManagement vehicleManagement) {
        List<VehicleManagement> list = vehicleManagementService.selectList(vehicleManagement);
        ExcelUtil<VehicleManagement> util = new ExcelUtil<>(VehicleManagement.class);
        util.hideColumn("updateTime", "remark");
        util.exportExcel(response, list, "监管对象管理");
    }

    /**
     * 获取监管对象详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(vehicleManagementService.selectOne(id));
    }

    /**
     * 新增监管对象
     */
    @ApiOperation("新增监管对象")
    @ApiOperationSupport(includeParameters = {"objIcon","companyId","numberPlate","vehicleModel","vehicleColor","vehiclePic","age","ownerSex","idCard","contactNumber"})
    @Log(title = "监管对象管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehicleManagement vehicleManagement) {
        return toAjax(vehicleManagementService.save(vehicleManagement));
    }

    @ApiOperation("编辑监管对象")
    @ApiOperationSupport(includeParameters = {"id","objIcon","companyId","numberPlate","vehicleModel","vehicleColor","vehiclePic","age","ownerSex","idCard","contactNumber"})
    @Log(title = "监管对象管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehicleManagement param) {
        return toAjax(vehicleManagementService.updateVehicle(param));
    }

    /**
     * 删除监管对象
     */
    @ApiOperation("删除监管对象")
    @Log(title = "监管对象管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        vehicleManagementService.deleteVehicle(ids);
        return success();
    }

    @ApiOperation("通过车牌号查设备号")
    @GetMapping("/queryDeviceByPlate")
    public AjaxResult queryDeviceByPlate(@RequestParam String numberPlate) {
        return success(vehicleManagementService.listByNumberPlate(numberPlate));
    }

    @ApiOperation("到期设备数量")
    @GetMapping("/expireCount")
    public AjaxResult expireCount() {
        int total = vehicleManagementService.getExpireVehicleCount(new VehicleManagement().setFailureTime(new Date()));
        return success(total);
    }

    @ApiOperation("监管对象下拉列表")
    @PostMapping("/search")
    public AjaxResult search(@Validated @RequestBody VehicleSearchParam param) {
        List<VehicleManagement> resultList = vehicleManagementService.selectList(new VehicleManagement()
                .setCompanyId(param.getCompanyId())
                .setNumberPlate(param.getNumberPlate())
                .setObjType(param.getObjType())
                .setBindStatus(BindStatusEnum.UNBINDING.getCode()));
        if (param.getEquipmentId() != null){
            EquipmentInfo equipmentInfo = equipmentManagementService.selectOne(param.getEquipmentId());
            if (equipmentInfo != null && StringUtils.isNotEmpty(equipmentInfo.getNumberPlate())
                    && equipmentInfo.getNumberPlate().contains(param.getNumberPlate())) {
                resultList.add(equipmentInfo.toObject());
            }
        }
        return success(resultList);
    }

    @ApiOperation("监管对象名称重复校验")
    @PostMapping("/checkExistNumberPlate")
    public AjaxResult checkExistNumberPlate(@Validated @RequestBody NumberPlateExistParam param) {
        vehicleManagementService.checkExistNumberPlate(param);
        return success();
    }

    @ApiOperation("文件上传（支持多文件）")
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestPart MultipartFile[] file){
        return success(uploadFile(file, "object"));
    }
}
